package com.yunquan.ohana.receiver

import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import cn.jpush.android.api.CmdMessage
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.google.gson.Gson
import com.yunquan.ohana.base.bus.RxBus
import com.yunquan.ohana.entity.EventEntity
import com.yunquan.ohana.entity.EventType
import com.yunquan.ohana.entity.PushBaseEntity
import com.yunquan.ohana.entity.PushEntity
import com.yunquan.ohana.utils.L


class MyJPushMessageReceiver : JPushMessageReceiver() {

    private var nm: NotificationManager? = null

    override fun onRegister(p0: Context?, p1: String?) {
        super.onRegister(p0, p1)
        L.d("onRegister: $p1")
    }

    override fun onMessage(p0: Context?, p1: CustomMessage?) {
        super.onMessage(p0, p1)
        L.d("onMessage: $p1")
    }

    override fun onConnected(p0: Context?, p1: Boolean) {
        super.onConnected(p0, p1)
        L.d("onConnected: $p1")
    }

    override fun onAliasOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onAliasOperatorResult(p0, p1)
        L.d("onAliasOperatorResult: $p1")
    }

    override fun onCheckTagOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onCheckTagOperatorResult(p0, p1)
        L.d("onCheckTagOperatorResult: $p1")
    }

    override fun onCommandResult(p0: Context?, p1: CmdMessage?) {
        super.onCommandResult(p0, p1)
        L.d("onCommandResult: $p1")
    }

    override fun onGeofenceReceived(p0: Context?, p1: String?) {
        super.onGeofenceReceived(p0, p1)
        L.d("onGeofenceReceived: $p1")
    }

    override fun onInAppMessageClick(p0: Context?, p1: NotificationMessage?) {
        super.onInAppMessageClick(p0, p1)
        L.d("onInAppMessageClick: $p1")
    }

    override fun onInAppMessageShow(p0: Context?, p1: NotificationMessage?) {
        super.onInAppMessageShow(p0, p1)
        L.d("onInAppMessageShow: $p1")
    }

    override fun onMobileNumberOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onMobileNumberOperatorResult(p0, p1)
        L.d("onMobileNumberOperatorResult: $p1")
    }

    override fun onMultiActionClicked(p0: Context?, p1: Intent?) {
        super.onMultiActionClicked(p0, p1)
        L.d("onMultiActionClicked: $p1")
    }

    override fun onNotifyMessageArrived(p0: Context?, p1: NotificationMessage?) {
        super.onNotifyMessageArrived(p0, p1)
        // APP 端
        // 1、相框信息更改，只发在线，参数：deviceId,name,location
        // 2、相框清空相册，暂时不做
        // 3、相框接收状态，整组文件接收完后推送，在线、离线均发送，参数：deviceId,groupUid
        // 4、相框在线状态，相框在线状态发生改变推送，只发在线，参数：deviceId,online
        // 5、解绑⽤户，只发在线，参数deviceId
        L.d("onNotifyMessageArrived: $p1")
        // {"msgType":1,"data":{"deviceId":"","groupUid":"","online":true,"name":"","location":""}}
        //{"params":"{\"data\":{\"id\":1901555615038570497},\"msgType\":5}"}
        val extras = p1?.notificationExtras
        if (extras != null) {
            val bean = Gson().fromJson(extras, PushBaseEntity::class.java)
            if (bean?.params != null) {
                val params = Gson().fromJson(bean.params, PushEntity::class.java)
                when (params.msgType) {
                    1 -> {
                        RxBus.getDefault().post(EventEntity(EventType.UPDATE_FRAMES_INFO, params.data))
                    }

                    2 -> {
                    }

                    3 -> {
                        RxBus.getDefault().post(EventEntity(EventType.UPDATE_FRAMES_RECEIVE, params.data))
                    }

                    4 -> {
                        RxBus.getDefault().post(EventEntity(EventType.UPDATE_FRAMES_ONLINE, params.data))
                    }

                    5 -> {
                        RxBus.getDefault().post(EventEntity(EventType.UNBIND_FRAMES, params.data))
                    }

                    6->{
                        RxBus.getDefault().post(EventEntity(EventType.BIND_FRAMES, params.data))
                    }
                }
            }
        }
    }

    override fun onNotifyMessageDismiss(p0: Context?, p1: NotificationMessage?) {
        super.onNotifyMessageDismiss(p0, p1)
        L.d("onNotifyMessageDismiss: $p1")
    }

    override fun onNotifyMessageOpened(p0: Context?, p1: NotificationMessage?) {
        super.onNotifyMessageOpened(p0, p1)
        L.d("onNotifyMessageOpened: $p1")
    }

    override fun onNotifyMessageUnShow(p0: Context?, p1: NotificationMessage?) {
        super.onNotifyMessageUnShow(p0, p1)
        L.d("onNotifyMessageUnShow: $p1")
    }

    override fun onPropertyOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onPropertyOperatorResult(p0, p1)
        L.d("onPropertyOperatorResult: $p1")
    }

    override fun onTagOperatorResult(p0: Context?, p1: JPushMessage?) {
        super.onTagOperatorResult(p0, p1)
        L.d("onTagOperatorResult: $p1")
    }

    override fun onGeofenceRegion(p0: Context?, p1: String?, p2: Double, p3: Double) {
        super.onGeofenceRegion(p0, p1, p2, p3)
        L.d("onGeofenceRegion: $p1")
    }

    override fun onNotificationSettingsCheck(p0: Context?, p1: Boolean, p2: Int) {
        super.onNotificationSettingsCheck(p0, p1, p2)
        L.d("onNotificationSettingsCheck: $p1")
    }

    override fun getNotification(p0: Context?, p1: NotificationMessage?): Notification {
        L.d("getNotification: $p1")
        return super.getNotification(p0, p1)
    }

}